package com.spark.core.transformation

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
 * sortByKey 默认按照key去排序，作用在K,V格式的RDD上
 */
object Demo6_sortByKey {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf()
    conf.setAppName("sortByKey")
    conf.setMaster("local")
    val sc = new SparkContext(conf)
    sc.setLogLevel("error")

    val infos = sc.parallelize(Array[(String, String)](
      ("f", "f"),
      ("a", "a"),
      ("c", "c"),
      ("b", "b")
    ))

    println(infos.getNumPartitions)

    // sortByKey会参数shuffle，可以指定分区数
    val result: RDD[(String, String)] = infos.sortByKey(false, 2)

    println(result.getNumPartitions)
    result.foreach(println)

  }
}
